#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

typedef pair<int, int> PII;
#define MAX_N 100000
long long arr[MAX_N + 5] = {0};
int l[MAX_N + 5] ={0}, r[MAX_N + 5] = {0}, st[MAX_N + 5] = {0};

void del(int x){
     r[l[x]] = r[x];
     l[r[x]] = l[x];
     st[x] = 1;
     return;
}

int main(){
    set<PII> h;
    int n, m, k = 1;
    scanf("%d%d", &n, &m);
    scanf("%lld", arr + 1);
    for(int i = 1, a; i < n; i++){
        scanf("%d", &a);
        if(1LL * arr[k] * a < 0) arr[++k] = a;
        else arr[k] += a;
    }
    n = k;
    long long cnt = 0, S = 0;
    for(int i = 1; i <= n; i++){
        if(arr[i] > 0){
            cnt += 1;
            S += arr[i];
        }
        h.insert(PII(abs(arr[i]), i));
        l[i] = i - 1;
        r[i] = i + 1;
    }
    while(cnt > m){
        while(st[h.begin()->second]) h.erase(h.begin());
        int idx = h.begin()->second;
        h.erase(h.begin());
        int left = l[idx], right = r[idx];
        if((left > 0 && right <= n) || arr[idx] > 0){
            S -= abs(arr[idx]);
            cnt -= 1;
            arr[idx] += arr[left] + arr[right];
            h.insert(PII(abs(arr[idx]), idx));
            del(left);
            del(right);
        }
    }
    
    printf("%lld\n", S);

    return 0;
}